﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using WF.Common;

namespace WF.Web.Controls
{
    public class CCHRButton : Button
    {
        public string OperateNo
        {
            get
            {
                if (null == this.ViewState["OperateNo"])
                {
                    return null;
                }
                else
                {
                    return (string)this.ViewState["OperateNo"];
                }
            }
            set
            {
                this.ViewState["OperateNo"] = value;
            }
        }

        public bool IsProtect
        {
            get
            {
                if (null == this.ViewState["IsProtect"])
                {
                    return false;
                }
                else
                {
                    return (bool)this.ViewState["IsProtect"];
                }
            }
            set
            {
                this.ViewState["IsProtect"] = value;
            }
        }

        public string CurrentDeptID
        {
            get
            {
                if (null == this.ViewState["CurrentDeptID"])
                {
                    return null;
                }
                else
                {
                    return (string)this.ViewState["CurrentDeptID"];
                }
            }
            set
            {
                this.ViewState["CurrentDeptID"] = value;
            }
        }

        public string CurrentDeptIDs
        {
            get
            {
                if (null == this.ViewState["CurrentDeptIDs"])
                {
                    return null;
                }
                else
                {
                    return (string)this.ViewState["CurrentDeptIDs"];
                }
            }
            set
            {
                this.ViewState["CurrentDeptIDs"] = value;
            }
        }

        public string CurrentEmpID
        {
            get
            {
                if (null == this.ViewState["CurrentEmpID"])
                {
                    return null;
                }
                else
                {
                    return (string)this.ViewState["CurrentEmpID"];
                }
            }
            set
            {
                this.ViewState["CurrentEmpID"] = value;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (IsProtect)
            {
                List<string> depts = new List<string>();
                if (!string.IsNullOrEmpty(CurrentDeptIDs))
                {
                    depts.AddRange(CurrentDeptIDs.Split(','));
                }
                if (!string.IsNullOrEmpty(CurrentDeptID))
                {
                    depts.Add(CurrentDeptID);
                }

                BusinessObject.SecurityManager sm = new BusinessObject.SecurityManager();
                AuthDeptRange resultAuth = AuthDeptRange.无;

                if (depts.Count == 0)
                {
                    resultAuth = sm.IsOperateAuthoried(this.OperateNo, this.CurrentDeptID, this.CurrentEmpID);
                }
                else
                {
                    foreach (string dept in depts)
                    {
                        AuthDeptRange auth = sm.IsOperateAuthoried(this.OperateNo, this.CurrentDeptID, this.CurrentEmpID);
                        {
                            if (auth > resultAuth)
                            {
                                resultAuth = auth;
                            }
                        }
                    }
                }

                if (resultAuth < AuthDeptRange.本人)
                {
                    this.Visible = false;
                }
            }
        }



    }
}